package com.reggie.controller.admin;

import com.reggie.annotation.IgnoreToken;
import com.reggie.constant.JwtClaimsConstant;
import com.reggie.constant.MessageConstant;
import com.reggie.constant.StatusConstant;
import com.reggie.dto.EmployeeDTO;
import com.reggie.dto.EmployeeLoginDTO;
import com.reggie.dto.EmployeePageQueryDTO;
import com.reggie.dto.PasswordEditDTO;
import com.reggie.entity.Employee;
import com.reggie.properties.JwtProperties;
import com.reggie.result.PageResult;
import com.reggie.result.R;
import com.reggie.service.EmployeeService;
import com.reggie.utils.JwtUtil;
import com.reggie.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.annotation.Documented;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/admin/employee")
@Api(tags = "员工相关的接口")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 测试拦截器是否起作用
     * @return
     */
    @GetMapping("/testJwt")
    public R<String> testJwt1() {
        return R.success("jwt test");
    }

    /**
     * 测试拦截器是否起作用
     * 测试自定义注解是否可用
     * @return
     */
    @PostMapping("/testJwt")
    @IgnoreToken//该注解只是用来标记
    public R<String> testJwt2(){
        return R.success("jwt test");
    }

    /**
     * 登录功能
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("员工登录")
    public R<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO){

        log.info("员工登录：" + employeeLoginDTO);
        //调用employeeService实现查询用户
        Employee employee = employeeService.login(employeeLoginDTO);

        //设置jwt中有效载荷部分的数据，通常是用户的身份标识
        Map<String,Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID,employee.getId());

        //创建jwt令牌
        String token = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(), claims);

        //将数据存入
        EmployeeLoginVO loginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .name(employee.getName())
                .userName(employee.getUsername())
                .token(token)
                .build();

        return R.success(loginVO);
    }

    /**
     * 退出系统
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("员工退出登录")
    public R<String> logout(){
        return R.success();
    }

    /**
     * 添加员工
     * @param employeeDTO
     * @return
     */
    @ApiOperation("新增员工接口")
    @PostMapping
    public R<String> save(@RequestBody EmployeeDTO employeeDTO){

        log.info("新增员工：{}",employeeDTO);
        //调用service实现添加功能
        employeeService.save(employeeDTO);

        return R.success();
    }

    /**
     * 分页功能
     */
    @GetMapping("/page")
    @ApiOperation("分页查询接口")
    public R<PageResult> pageQuery(EmployeePageQueryDTO employeePageQueryDTO){
        log.info("分页查询：" + employeePageQueryDTO);
        return R.success(employeeService.page(employeePageQueryDTO));
    }

    /**
     * 根据id查询数据
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询员工")
    public R<Employee> findById(@PathVariable Long id){
        log.info("根据id查询员工：" + id);
        Employee employee = employeeService.findById(id);
        return R.success(employee);
    }

    /**
     * 修改员工信息
     * @param employeeDTO
     * @return
     */
    @PutMapping
    @ApiOperation("编辑员工信息接口")
    public R<String> update(@RequestBody EmployeeDTO employeeDTO){
        log.info("编辑员工信息：" + employeeDTO);
        employeeService.update(employeeDTO);
        return R.success();
    }

    /**
     * 员工的启用禁用
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("员工启用禁用接口")
    public R<String> startOrStop(@PathVariable Integer status, Long id){
        log.info("员工启用禁用：{},{}" + status,id );
        employeeService.starOrStop(status,id);
        return R.success();
    }

    @PutMapping("/editPassword")
    @ApiOperation("修改密码功能")
    public R<String> upPassword(@RequestBody PasswordEditDTO passwordEditDTO){
        employeeService.upPassword(passwordEditDTO);
        return R.success();
    }
}
